﻿namespace Known.FreeSql;

/// <summary>
/// 依赖注入扩展类。
/// </summary>
public static class Extension
{
    /// <summary>
    /// 添加支持 FreeSql 数据访问。
    /// </summary>
    /// <param name="services">服务集合。</param>
    /// <param name="action">FreeSql 配置。</param>
    public static void AddKnownFreeSql(this IServiceCollection services, Action<FreeSqlBuilder> action)
    {
        services.AddScoped<Database, FreeSqlDatabase>();

        Func<IServiceProvider, IFreeSql> factory = r =>
        {
            var builder = new FreeSqlBuilder();
            action?.Invoke(builder);
            return builder.Build();
        };
        services.AddSingleton(factory);
        FreeSqlHelper.Provider = services.BuildServiceProvider();
    }
}